package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.entity.LendReturn;
import com.atguigu.srb.core.service.LendReturnService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/api/core/lendReturn")
@Api(tags = "还款管理")
public class ApiLendReturnController {
    @Resource
    LendReturnService lendReturnService;
    @ApiOperation("还款计划的查询和展示")
    @GetMapping("/returnPlan/{lendId}")
    public R getReturnPlan(@PathVariable("lendId")Long lendId){
        //这个查询不区分还款和未还款的,按照升序的来展示  /admin/core/lendReturn
        List<LendReturn> list = lendReturnService.list(Wrappers.lambdaQuery(LendReturn.class)
                        .eq(LendReturn::getLendId,lendId)
                .orderByAsc(LendReturn::getCurrentPeriod));
        return R.ok().data("items",list);
    }
    //还款同步接口,需要的参数,用户的token,当前还款计划的id
    @ApiOperation("还款的同步接口")
    @PostMapping("/auth/repayment/{lendReturnId}")
    public R repayment(@RequestHeader(value = "token",required = false)String token,
                       @PathVariable("lendReturnId")Long lendReturnId){
        String form = lendReturnService.repayment(token,lendReturnId);
        return R.ok().data("form",form);
    }
    //还款的异步通知接口 /api/core/lendReturn/notifyUrl
    @ApiOperation("还款的异步通知接口")
    @PostMapping("/notifyUrl")
    public String repaymentNotify(@RequestParam Map<String,Object> params){
        return lendReturnService.repaymentNotify(params);
    }
}

